/**
 * Date:2021年9月6日 下午8:26:34
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com.xue.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xue.dao.ds1.Product1;
import com.xue.dao.ds1.ProductRepository1;
import com.xue.dao.ds2.Product2;
import com.xue.dao.ds2.ProductRepository2;

/**
 * Description: TODO <br/>
 * Date:2021年9月6日 下午8:26:34 <br/>
 * 
 * @author xuejianxin
 *        
 */

@Service
public class AppService {

	/*
	 * private JdbcTemplate ds1JdbcTemplate; private JdbcTemplate ds2JdbcTemplate;
	 * 
	 * public AppService(JdbcTemplate ds1JdbcTemplate, JdbcTemplate ds2JdbcTemplate)
	 * { this.ds1JdbcTemplate = ds1JdbcTemplate; this.ds2JdbcTemplate =
	 * ds2JdbcTemplate; }
	 * 
	 * @Transactional public void test1() { // 修改test1库中的数据
	 * ds1JdbcTemplate.update("update product set title = ? where id = ?", "xue ok",
	 * 1); // 修改test2库中的数据
	 * ds2JdbcTemplate.update("update product set title = ? where id = ?", "xue ok",
	 * 1); }
	 * 
	 * @Transactional public void test2() { // 修改test1库中的数据
	 * ds1JdbcTemplate.update("update product set title = ? where id = ?", "xue222",
	 * 1); // 修改test2库中的数据
	 * ds2JdbcTemplate.update("update product set title = ? where id = ?", "xue222",
	 * 1); // 模拟：修改test2库之前抛出异常 throw new RuntimeException(); }
	 */

	@Autowired
	private ProductRepository1 productRepository1;

	@Autowired
	private ProductRepository2 productRepository2;

	private void test(String flag) {
		String msg = "test " + flag;
		Product1 p1 = new Product1();
		p1.setTitle(msg);
		p1.setPrice(100.0);
		p1.setDescription(msg);
		productRepository1.save(p1);

		Product2 p2 = new Product2();
		p2.setTitle(msg);
		p2.setPrice(100.0);
		p2.setDescription(msg);
		productRepository2.save(p2);
	}

	@Transactional
	public void test1() {
		test("success");

	}

	@Transactional
	public void test2() {
		test("error");
		System.out.println(1/0);
		//throw new RuntimeException("error");
	}

}
